package org.javarosa.activity.mesh4j.extensions;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import org.javarosa.core.Context;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.ICommand;
import org.javarosa.core.api.IShell;
import org.javarosa.core.services.UnavailableServiceException;
import org.javarosa.core.services.mesh4j.extensions.SyncEngineService;
import org.javarosa.core.services.transport.TransportMessage;
import org.javarosa.formmanager.view.SubmitStatusScreen;
import org.mesh4j.sync.validations.MeshException;

public class ImportFormDefinitionActivity implements IActivity, CommandListener{
	
	// MODEL VARIABLES
	private Context context;
	private IShell shell;
	private SubmitStatusScreen submitStatusScreen;
	private int counter;
	private Timer timer;
	protected int sendStatus;
	protected boolean useHttp = true;
	
	// BUSINESS METHODS
	
	public ImportFormDefinitionActivity(IShell mainShell) {
		this.shell = mainShell;
	}

	public void contextChanged(Context globalContext) {
		context.mergeInContext(globalContext);
	}

	public void destroy() {
		// Nothing for this Module
	}

	public void halt() {
		// Nothing for this Module
	}

	public void resume(Context globalContext) {	
		this.importFormDef();
	}

	public void start(Context context) {
		this.context = context;
		this.importFormDef();
	}

	public void commandAction(Command c, Displayable d) {
		if (d == submitStatusScreen) {
			this.timer.cancel();
			this.timer = null;
			
			this.shell.returnFromActivity(this, Constants.ACTIVITY_COMPLETE, null);
		}
	}

	public void setHttpProtocol() {
		this.useHttp = true;
	}

	public void setSmsProtocol() {
		this.useHttp = false;
	}
	
	private void importFormDef() {
		this.sendStatus = TransportMessage.STATUS_NEW;
		
		submitStatusScreen = new SubmitStatusScreen(this);
		submitStatusScreen.setCommandListener(this);
		
		timer = new Timer();
		timer.schedule(new TimerTask () {
				public void run () {
					if(ImportFormDefinitionActivity.this.sendStatus == TransportMessage.STATUS_NEW){
						counter += SubmitStatusScreen.REFRESH_INTERVAL;
					}
					submitStatusScreen.updateStatus(ImportFormDefinitionActivity.this.sendStatus);
				}
			}, 
			SubmitStatusScreen.REFRESH_INTERVAL, 
			SubmitStatusScreen.REFRESH_INTERVAL
		);

		shell.setDisplay(this, submitStatusScreen);
		
		new WorkerThread().start();
	}
	
	
	private class WorkerThread implements Runnable {
		
		public void start(){
			new Thread(this).start();
		}
		
		public void run() {
			if(useHttp){
				importFormDefFromURL();
			} else {
				importFormDefFromSMS();
			}
		}
	}
	
	private void importFormDefFromURL() {
		try{
			this.sendStatus = TransportMessage.STATUS_NEW;
			SyncEngineService.importFormDefFromURL();
			this.sendStatus = TransportMessage.STATUS_RECEIVED;
		} catch (MeshException e) {
			e.printStackTrace();
			this.sendStatus = TransportMessage.STATUS_FAILED;
		}
	}
	
	
	private void importFormDefFromSMS() {
		try{
			this.sendStatus = TransportMessage.STATUS_NEW;
			SyncEngineService.importFormDefFromSMS();
			this.sendStatus = TransportMessage.STATUS_RECEIVED;
		} catch (MeshException e) {
			e.printStackTrace();
			this.sendStatus = TransportMessage.STATUS_FAILED;
		} catch (UnavailableServiceException e) {
			e.printStackTrace();
			this.sendStatus = TransportMessage.STATUS_FAILED;
		}
	}

	public Context getActivityContext() {
		return this.context;
	}

	public void setShell(IShell shell) {
		this.shell = shell;
	}
	/* (non-Javadoc)
	 * @see org.javarosa.core.api.IActivity#annotateCommand(org.javarosa.core.api.ICommand)
	 */
	public void annotateCommand(ICommand command) {
		throw new RuntimeException("The Activity Class " + this.getClass().getName() + " Does Not Yet Implement the annotateCommand Interface Method. Please Implement It.");
	}
}
